#include "sphereCube.h"
#include "math.h"
#include "drawing.h"

#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#include "OpenGl\gl.h"
#include "OpenGl\glu.h"

namespace Tmpl {

Sphere::Sphere()
{
	mQuadCount = 0;

	// Create the 6 quads
	mQuads[mQuadCount++] = Quad( Vec3f(-1.0f, -1.0f,  1.0f), Vec2f(0.0f, 0.0f), 
								 Vec3f( 1.0f, -1.0f,  1.0f), Vec2f(1.0f, 0.0f),
								 Vec3f( 1.0f,  1.0f,  1.0f), Vec2f(1.0f, 1.0f),
								 Vec3f(-1.0f,  1.0f,  1.0f), Vec2f(0.0f, 1.0f));

	mQuads[mQuadCount++] = Quad( Vec3f(-1.0f, -1.0f, -1.0f), Vec2f(1.0f, 0.0f), 
								 Vec3f(-1.0f,  1.0f, -1.0f), Vec2f(1.0f, 1.0f),
								 Vec3f( 1.0f,  1.0f, -1.0f), Vec2f(0.0f, 1.0f),
								 Vec3f( 1.0f, -1.0f, -1.0f), Vec2f(0.0f, 0.0f));

	mQuads[mQuadCount++] = Quad( Vec3f(-1.0f,  1.0f, -1.0f), Vec2f(0.0f, 1.0f), 
								 Vec3f(-1.0f,  1.0f,  1.0f), Vec2f(0.0f, 0.0f),
								 Vec3f( 1.0f,  1.0f,  1.0f), Vec2f(1.0f, 0.0f),
								 Vec3f( 1.0f,  1.0f, -1.0f), Vec2f(1.0f, 1.0f));

	mQuads[mQuadCount++] = Quad( Vec3f(-1.0f, -1.0f, -1.0f), Vec2f(1.0f, 1.0f), 
								 Vec3f( 1.0f, -1.0f, -1.0f), Vec2f(0.0f, 1.0f),
								 Vec3f( 1.0f, -1.0f,  1.0f), Vec2f(0.0f, 0.0f),
								 Vec3f(-1.0f, -1.0f,  1.0f), Vec2f(1.0f, 0.0f));

	mQuads[mQuadCount++] = Quad( Vec3f( 1.0f, -1.0f, -1.0f), Vec2f(1.0f, 0.0f), 
								 Vec3f( 1.0f,  1.0f, -1.0f), Vec2f(1.0f, 1.0f),
								 Vec3f( 1.0f,  1.0f,  1.0f), Vec2f(0.0f, 1.0f),
								 Vec3f( 1.0f, -1.0f,  1.0f), Vec2f(0.0f, 0.0f));

	mQuads[mQuadCount++] = Quad( Vec3f(-1.0f, -1.0f, -1.0f), Vec2f(0.0f, 0.0f), 
								 Vec3f(-1.0f, -1.0f,  1.0f), Vec2f(1.0f, 0.0f),
								 Vec3f(-1.0f,  1.0f,  1.0f), Vec2f(1.0f, 1.0f),
								 Vec3f(-1.0f,  1.0f, -1.0f), Vec2f(0.0f, 1.0f));
}

// there will be 4^(levels+1) faces in the sphere
void Sphere::SubDivide()
{
}

static Image* tex = 0;
void Sphere::Render()
{  
	if (!tex)
	{
		tex = new Image();
		tex->Create("Data/checkerboard.png");
		tex->BuildTex();
	}
	tex->BindTexture();

	// Render the Texture
	glBegin(GL_QUADS);
	for (int i=0; i<mQuadCount; ++i)
	{
		Quad* t = &mQuads[i];

		glTexCoord2f(t->uv0.x, t->uv0.y);
		glVertex3f(t->v0.x, t->v0.y, t->v0.z);

		glTexCoord2f(t->uv1.x, t->uv1.y);
		glVertex3f(t->v1.x, t->v1.y, t->v1.z);

		glTexCoord2f(t->uv2.x, t->uv2.y);
		glVertex3f(t->v2.x, t->v2.y, t->v2.z);

		glTexCoord2f(t->uv3.x, t->uv3.y);
		glVertex3f(t->v3.x, t->v3.y, t->v3.z);
	}
	glEnd();

	/*
	// Render the UV
	glBegin(GL_TRIANGLES);
	for (int i=0; i<mTriangleCount; ++i)
	{
		Triangle* t = &mTriangles[i];

		glColor3f(t->uv0.x, t->uv0.y, 0);
		glVertex3f(t->v0->pos.x, t->v0->pos.y, t->v0->pos.z);

		glColor3f(t->uv1.x, t->uv1.y, 0);
		glVertex3f(t->v1->pos.x, t->v1->pos.y, t->v1->pos.z);

		glColor3f(t->uv2.x, t->uv2.y, 0);
		glVertex3f(t->v2->pos.x, t->v2->pos.y, t->v2->pos.z);
	}
	glEnd();
	*/

	/*
	// Render the Lines
	glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
	glColor3f(0,1,0);
	glBegin(GL_TRIANGLES);
	for (int i=0; i<mTriangleCount; ++i)
	{
		Triangle* t = &mTriangles[i];
		glVertex3f(t->v0->pos.x, t->v0->pos.y, t->v0->pos.z);
		glVertex3f(t->v1->pos.x, t->v1->pos.y, t->v1->pos.z);
		glVertex3f(t->v2->pos.x, t->v2->pos.y, t->v2->pos.z);
	}
	glEnd();
	glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
	glColor3f(1,1,1);
	*/

	/*
	// Render the Points
	glPointSize(10.0f);
	glBegin(GL_POINTS);
	for (int i=0; i<mVertexCount; ++i)
	{
		Vertex* v = &mVertices[i];
		glVertex3f(v->pos.x, v->pos.y, v->pos.z);
	}
	glEnd();
	glPointSize(1.0f);
	*/
}

}; // namespace Tmpl